<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class once_controller extends wxapp_controller{
	//wxapp招聘店铺列表
	function list_action(){//职位列表
		$where="status='1' and `pay`<>1  AND `edate`>'".time()."'";
		$sdate=$_POST['sdate'];
		$edate=$_POST['edate'];
		$keyword=$this->stringfilter($_POST['keyword']);
		$page=$_POST['page'];
		$limit=$_POST['limit'];
		$order=$_POST['order'];
		$nodata=$_POST['nodata'];
		$limit=!$limit?10:$limit;
		if($sdate){//开始时间
			$where.=" and `ctime`>'".strtotime($sdate)."'";
		}
		if($edate){//结束时间
			$where.=" and `ctime`<'".strtotime($edate)."'";
		}
		if($keyword){//关键字
			$where.=" and `title` like '%".$keyword."%'";
		}
		if($nodata){//排除没有值的字段
			$nodataarr=explode(",",$nodata);
			foreach($nodataarr as $v){
				$where.=" and ".$v."<>''";
			}
		}
		if($order){//排序
			$where.=" order by ".$order;
		}else{
			$where.=" order by ctime desc";
		}
		if($page){//分页
			$pagenav=($page-1)*$limit;
			$where.=" limit $pagenav,$limit";
		}else{
			$where.=" limit $limit";
		}
		$rows=$this->obj->DB_select_all("once_job",$where);
		if(is_array($rows)&&!empty($rows)){
			foreach($rows as $key=>$va){
				$list[$key]['id']		   =$va['id'];
				$list[$key]['title']	   =$va['title'];
				$list[$key]['mans']		   =$va['mans'];
				$list[$key]['require']	   =$va['require'];
				$list[$key]['phone']	   =$va['phone'];
				$list[$key]['companyname'] =$va['companyname'];
				$list[$key]['linkman']	   =mb_substr($va['linkman'], 0,5,'utf-8');
				$list[$key]['address']     =$va['address'];
				$list[$key]['ctime_n']	   =date("Y-m-d",$va['ctime']);
			}
			foreach($list as $k=>$v){
				if(is_array($v)){
					foreach($v as $key=>$val){
						$list[$k][$key]=isset($val)?$val:'';
					}
				}else{
					$list[$k]=isset($v)?$v:'';
				}
			}
			$data['user_wzp_link']=$this->config['user_wzp_link'];
			$data['list']=count($list)?$list:array();
			$data['error']=1;
		}else{
			$data['error']=2;
		}
		echo json_encode($data);die;
	}
	//wxapp招聘店铺详情页
	function show_action(){//职位内容页
		$id=(int)$_POST['id'];
		if(!$id){
			$data['error']=3;
			echo json_encode($data);die;
		}
		
		$TinyM=$this->MODEL('once');
		$TinyM->UpdateOncejob(array("`hits`=`hits`+1"),array('id'=>(int)$id));
		$row=$this->obj->DB_select_once("once_job","`id`='".$id."' and `edate`>'".time()."'");
		if(is_array($row)){
			$list['id']	=$row['id'];
			$list['title']	=$row['title'];
			$list['hits']	=intval($row['hits']);
			$list['mans']	=$row['mans'];
			$list['require']=$row['require'];
			$list['phone']	=$row['phone'];
			$list['companyname']	=$row['companyname'];
			$list['linkman']=$row['linkman'];
			$list['address']=$row['address'];
			$list['salary']=$row['salary'];
			$list['ctime_n']=date("Y-m-d",$row['ctime']);
			$list['edate_n']=date("Y-m-d",$row['edate']);
			$list['days']	=intval((intval($row['edate'])-time())/86400).'';
			if($row['pic'] && (file_exists(APP_PATH.$row['pic']))){
			    $list['pic']	=$this->config['sy_weburl'].'/'.$row['pic'];
			}
			foreach($list as $k=>$v){
				if(is_array($v)){
					foreach($v as $key=>$val){
						$list[$k][$key]=isset($val)?$val:'';
					}
				}else{
					$list[$k]=isset($v)?$v:'';
				}
			}
			$data['user_wzp_link']=$this->config['user_wzp_link'];
			$data['list']=count($list)?$list:array();
			$data['error']=1;
		}else{
			$data['error']=2;
		}
		echo json_encode($data);die;
	}
	//wxapp招聘店铺添加修改
	function add_action(){
	    $_POST=$this->post_trim($_POST);
		if($_POST['submit']){
		    if(!$_POST['salary'] ||!$_POST['title'] || !$_POST['password'] || !$_POST['requires'] || !$_POST['companyname'] || !$_POST['phone'] || !$_POST['linkman']|| !$_POST['address']){
				$data['error']=3;
				$data['errmsg']='请完善信息！';
				echo json_encode($data);die;
			}

			if(is_uploaded_file($_FILES['photos']['tmp_name'])){
				$UploadM=$this->MODEL('upload');
				$upload=$UploadM->Upload_pic("../../data/upload/once/",false);
				$pictures=$upload->picture($_FILES['photos']);
				$pic=str_replace("../../data/upload/once/","data/upload/once/",$pictures);
				$sql['pic']=$pic;
			}

			$sql['title']=$this->stringfilter($_POST['title']);
			$sql['companyname']=$this->stringfilter($_POST['companyname']);
			$sql['linkman']=$this->stringfilter($_POST['linkman']);
			$sql['phone']=$this->stringfilter($_POST['phone']);
			$sql['require']=$this->stringfilter($_POST['requires']);
			$sql['address']=$this->stringfilter($_POST['address']);
 			$sql['edate']=strtotime("+".(int)$_POST['edate']." days");
			$sql['salary']=$this->stringfilter($_POST['salary']);
			$sql['password']=md5($_POST['password']);
			$sql['status']=$this->config['com_fast_status'];
			$sql['edate']=strtotime("+".(int)$_POST['edate']." days");
			$sql['ctime']=time();
			if($_POST['id']){
				$id=$this->obj->update_once("once_job",$sql,array("id"=>(int)$_POST['id']));
				if($id){
				    if($this->config['once_pay_price'] != "0" && $this->config['once_pay_price'] != ""){
				        $data['error']=10;
				        $data['errmsg']='店铺招聘需要支付'.$this->config['once_pay_price'].'元才能开始招聘';
				        $data['onceid']=(int)$_POST['id'];
				        echo json_encode($data);die;
				    }
					if($this->config['com_fast_status']=="1"){
						$data['error']=1;
						$data['errmsg']='修改成功！';
					}else{
						$data['error']=1;
						$data['errmsg']='修改成功，等待审核！';
					}
				}else{
					$data['error']=2;
					$data['errmsg']='修改失败！';
				}
			}else{
			    if($this->config['once_pay_price'] != "0" && $this->config['once_pay_price'] != ""){
			        $sql['pay']=1;
			    }
				$M=$this->MODEL('once');
			    $ip=fun_ip_get();
			    $start_time=strtotime('today'); //开始时间
			    $oncenum=$M->GetOncejobNum(array('login_ip'=>$ip,'`ctime`>\''.$start_time.'\''));
			    if($this->config['sy_once']>$oncenum||$this->config['sy_once']<1){
			        $nid=$this->obj->insert_into("once_job",$sql);
			        if($nid){
			            if($this->config['once_pay_price'] != "0" && $this->config['once_pay_price'] != ""){
			                $data['error']=10;
			                $data['errmsg']='店铺招聘需要支付'.$this->config['once_pay_price'].'元才能开始招聘';
			                $data['onceid']=$nid;
			                echo json_encode($data);die;
			            }
			            if($this->config['com_fast_status']=="1"){
			                $this->errjson('发布成功',1);
			            }else{
			                $this->errjson('发布成功，等待审核',1);
			            }
			        }else{
			            $this->errjson('提交失败',2);
			        }
			    }else{
			        $this->errjson("一天内只能发布".$this->config['sy_once']."次",2);
			    }
			}
		}else{
		    $row=$this->obj->DB_select_once("once_job","`id`='".(int)$_POST['id']."'");
		    $data['id']	=$row['id'];
		    $data['title']	=$row['title'];
		    $data['require']=$row['require'];
		    $data['phone']	=$row['phone'];
		    $data['companyname']	=$row['companyname'];
		    $data['linkman']=$row['linkman'];
		    $data['address']=$row['address'];
		    $data['salary']=$row['salary'];
		    if($row['ctime']&&$row['edate']){
		        $data['edate_n']=ceil(($row['edate']-$row['ctime'])/86400) ;
		    }
		    if($row['pic'] && (file_exists(APP_PATH.$row['pic']))){
		        $data['pic']=$this->config['sy_weburl']."/".$row['pic'];
		    }
		}
		echo json_encode($data);die;
	}
	
	function pass_action(){
		$id=(int)$_POST['id'];
		if(!$_POST['password'] || !$id){
			$data['error']=3;
			echo json_encode($data);die;
		}
		$row=$this->obj->DB_select_once("once_job","`id`='".$id."'","password");
		if(is_array($row)){
			if($row['password']==md5($_POST['password'])){
				$data['error']=1;
			}else{
				$data['error']=2;
			}
		}else{
			$data['error']=4;
		}
		echo json_encode($data);die;
	}

	//wxapp招聘店铺删除
	function del_action(){
		$id=(int)$_POST['id'];
		if(!$_POST['password'] || !$id){
			$data['error']=3;
			echo json_encode($data);die;
		}
		$row=$this->obj->DB_select_once("once_job","`id`='".$id."'","password");
		if(is_array($row)){
			if($row['password']==md5($_POST['password'])){
				$id=$this->obj->DB_delete_all("once_job","`id`='".$id."'");
				$data['error']=1;
			}else{
				$data['error']=2;
			}
		}else{
			$data['error']=4;
		}
		echo json_encode($data);die;
	}
	//wxapp招聘店铺刷新
	function editctime_action(){
		$id=(int)$_POST['id'];
		if(!$_POST['password'] || !$id){
			$data['error']=3;
			$data['id']=$_POST['password'];
			echo json_encode($data);die;
		}
		$row=$this->obj->DB_select_once("once_job","`id`='".$id."'","`password`,`sxnumber`");
		if(is_array($row)){
		    if($this->config['com_xin']>0 && $this->config['com_xin']>$row['sxnumber']){
		        if($row['password']==md5($_POST['password'])){
		            $sql['ctime']=time();
		            if ($row['sxnumber']){
		                $sql['`sxnumber`=`sxnumber`+1'];
		            }else{
		                $sql['sxnumber']=1;
		            }
		            $nid = $this->obj->update_once('once_job',$sql,array('id'=>$id));
		            if($nid){
		                $this->errjson('刷新成功',1);
		            }else{
		                $this->errjson('刷新失败',-1);
		            }
		        }else{
		            $this->errjson('密码错误',4);
		        }
		    }else{
		        $this->errjson('每份店铺招聘只能刷新'.$this->config['com_xin'].'次',5);
		    }
		}else{
		    $this->errjson('系统繁忙',6);
		}
	}
	function isadd_action(){
	    $data['sy_once']=$this->config['sy_once'];
	    //$data['once_pay_price']=$this->config['once_pay_price'];
	    $M=$this->MODEL('once');
	    $ip=fun_ip_get();
	    $start_time=strtotime('today'); //开始时间
	    $oncenum=$M->GetOncejobNum(array('login_ip'=>$ip,'`ctime`>\''.$start_time.'\''));
	    if($this->config['sy_once']>$oncenum||$this->config['sy_once']<1){
	        $data['isadd']=true;
	    }else{
	        $data['isadd']=false;
	    }
	    echo json_encode($data);die;
	}
	//发布时查询是否有未付款的店铺招聘
	function getunpay_action(){
	    $id=intval($_POST['id']);
	    $order=$this->obj->DB_select_once("company_order","`once_id`='".$id."' AND `order_state`=1",'`id`');
	    if($order){
	        $row=$this->obj->DB_select_once("once_job","`id`='".$id."'");
	        $data['id']	=$row['id'];
	        $data['title']	=$row['title'];
	        $data['require']=$row['require'];
	        $data['phone']	=$row['phone'];
	        $data['companyname']	=$row['companyname'];
	        $data['linkman']=$row['linkman'];
	        $data['address']=$row['address'];
	        $data['salary']=$row['salary'];
	        if($row['ctime']&&$row['edate']){
	            $data['edate_n']=ceil(($row['edate']-$row['ctime'])/86400) ;
	        }
	        if($row['pic'] && (file_exists(APP_PATH.$row['pic']))){
	            $data['pic']=$this->config['sy_weburl']."/".$row['pic'];
	        }
	        $data['error']=0;
	        $data['errmsg']='您有未付款的店铺招聘';
	    }else{
	        $data['error']=-1;
	    }
	    echo json_encode($data);die;
	}
	//删除未付款的店铺招聘
	function delunpay_action(){
	    $id=$_POST['onceid'];
	    $row=$this->obj->DB_select_once("once_job","`id`='".$id."'","`id`");
	    if ($row){
	        $this->obj->DB_delete_all('once_job',"`id`='".$id."'");
	        $this->obj->DB_delete_all('company_order',"`once_id`='".$id."'","");
	    }
	}
	function dingdan_action(){
	    $getdata=$this->getOpenid($_POST['code']);
	    $onceid=intval($_POST['onceid']);
	    $order=$this->obj->DB_select_once("company_order","`once_id`='".$onceid."' AND `order_state`=1",'`id`');
	    if ($order){
	        $this->obj->DB_delete_all('company_order',"`once_id`='".$onceid."'","");
	    }
	    if($getdata['openid']){
	        $price=$this->config['once_pay_price'];
	        $time = time();
	        $dingdan=$time.rand(10000,99999);
	        $orderData['type']='25';
	        $orderData['order_id']=$dingdan;
	        $orderData['order_price']=$price;
	        $orderData['order_time']=time();
	        $orderData['order_type']="wxpay";
	        $orderData['order_state']="1";
	        $orderData['order_remark']='店铺招聘收费';
	        $orderData['once_id']=$onceid;
	        $orderid=$this->obj->insert_into("company_order",$orderData);
	        if($orderid){
	            require_once(LIB_PATH.'wxOrder.function.php');
	            $data = wxXcxOrder(array('body'=>'充值','id'=>$dingdan,'url'=>$this->config['sy_weburl'],'total_fee'=>$price,'openid'=>$getdata['openid']));
	            echo $data;die;
	        }else{
	            $data['errmsg']="提交失败，请重新提交订单";
	            echo json_encode($data);die;
	        }
	    }else{
	        if ($getdata['errcode']){
	            $data['errmsg']='出错了，请重试';
	        }else{
	            $data['errmsg']='参数错误';
	        }
	    }
	}
	function adddingdan_action(){
	    $onceid=intval($_POST['onceid']);
	    $order=$this->obj->DB_select_once("company_order","`once_id`='".$onceid."' AND `order_state`=1",'`id`');
	    if ($order){
	        $this->obj->DB_delete_all('company_order',"`once_id`='".$onceid."'","");
	    }
	    $time = time();
	    $dingdan=$time.rand(10000,99999);
	    $orderData['type']='25';
	    $orderData['order_id']=$dingdan;
	    $orderData['order_price']=$this->config['once_pay_price'];
	    $orderData['order_time']=time();
	    $orderData['order_type']="wxpay";
	    $orderData['order_state']="1";
	    $orderData['order_remark']='店铺招聘收费';
	    $orderData['once_id']=$onceid;
	    $this->obj->insert_into("company_order",$orderData);
	}
}
?>